﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace block_explorer
{
    class Hud : DrawableGameComponent
    {
        private BlockExplorer mGame;
        private DebugTextStacker mTopLeftDebugText;
        private DebugTextStacker mBottomRightDebugText;
        private FPSCounter mFPSCounter;
 
        public Hud(BlockExplorer game)
            :base(game)
        {
            mGame = game;

            mTopLeftDebugText = new DebugTextStacker(game);
            mBottomRightDebugText = new DebugTextStacker(game);
            mFPSCounter = new FPSCounter(game);
        }

        public override void Initialize()
        {
            base.Initialize();

            
            mTopLeftDebugText.Position = new Vector2(0, 0);
            mTopLeftDebugText.Color = Color.Yellow;
            mTopLeftDebugText.Direction = DebugTextStacker.GrowDirection.DOWN;
            mTopLeftDebugText.HorizontalAnchor = DebugTextStacker.HorizAnchor.LEFT;

            
            mBottomRightDebugText.Position = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
            mBottomRightDebugText.Color = Color.Yellow;
            mBottomRightDebugText.Direction = DebugTextStacker.GrowDirection.UP;
            mBottomRightDebugText.HorizontalAnchor = DebugTextStacker.HorizAnchor.RIGHT;

            mTopLeftDebugText.DrawOrder = 1;
            mBottomRightDebugText.DrawOrder = 1;
            mFPSCounter.DrawOrder = 1;

            Game.Components.Add(mTopLeftDebugText);
            Game.Components.Add(mBottomRightDebugText);
            Game.Components.Add(mFPSCounter);
        }

        protected override void LoadContent()
        {
            mTopLeftDebugText.Font = Game.Content.Load<SpriteFont>("ConsoleFont");
            mBottomRightDebugText.Font = Game.Content.Load<SpriteFont>("ConsoleFont");

            base.LoadContent();
        }

        public override void Draw(GameTime gameTime)
        {
            mTopLeftDebugText.Clear();
            mBottomRightDebugText.Clear();

            Vector3 worldPos = mGame.Scene.SelectedCamera.Position;
            SectorAddress secAddress = SectorAddress.FromWorldPosition(worldPos);
            Vector3 localPos = worldPos - secAddress.ToWorldPosition();
            mTopLeftDebugText.AddLine("World = (" + worldPos.X + ", " + worldPos.Y + ", " + worldPos.Z + ")");
            mTopLeftDebugText.AddLine("Local = (" + localPos.X + ", " + localPos.Y + ", " + localPos.Z + ")");
            mTopLeftDebugText.AddLine("Sector = (" + secAddress + ")");

            GraphicsDevice.BlendState = BlendState.Opaque;
            GraphicsDevice.DepthStencilState = DepthStencilState.Default;

            //// vert and tri count of blocks
            //string vertCount = "Verts = " + mGame.World.Map.DrawnVerts.ToString("N0");
            //string triCount = "Tris = " + mGame.World.Map.DrawnTris.ToString("N0");
            //string secCount = "Sectors = " + mGame.World.Map.CachedSectors.ToString("N0");
            //string threadCount = "Running Jobs = " + mGame.World.Map.BuildThreadCount.ToString("N0");
            //string queueCount = "Queued Jobs = " + mGame.World.Map.QueuedThreadCount.ToString("N0");
            //string buildTime = "Avg Build Time = " + mGame.World.Map.AverageCacheBlocksTime.ToString("F2");
            //string meshTime = "Avg Mesh Time = " + mGame.World.Map.AverageRefreshMeshTime.ToString("F2");
            //string lightsTime = "Avg Lights Time = " + mGame.World.Map.AverageRefreshLightsTime.ToString("F2");

            //mBottomRightDebugText.AddLine(lightsTime);
            //mBottomRightDebugText.AddLine(meshTime);
            //mBottomRightDebugText.AddLine(buildTime);
            //mBottomRightDebugText.AddLine(queueCount);
            //mBottomRightDebugText.AddLine(threadCount);
            //mBottomRightDebugText.AddLine(secCount);
            //mBottomRightDebugText.AddLine(triCount);
            //mBottomRightDebugText.AddLine(vertCount);
        }
    }
}
